// Created: Dec 11, 2014
//
// Author: wjinwen.1988@gmail.com

#ifndef CHI_CONDITION_H
#define CHI_CONDITION_H

#include "chi/mutex.h"

#include <pthread.h>

namespace chi {

class Condition {
 public:
  explicit Condition(MutexLock& mutex)
    : mutex_(mutex) {
    pthread_cond_init(&pcond_, NULL);
  }

  ~Condition() {
    pthread_cond_destroy(&pcond_);
  }

  Condition(const Condition&) = delete;
  void operator= (const Condition&) = delete;

  void Wait() {
    pthread_cond_wait(&pcond_, mutex_.mutex());
  }

  void Notify() {
    pthread_cond_signal(&pcond_);
  }

  void NotifyAll() {
    pthread_cond_broadcast(&pcond_);
  }

 private:
  MutexLock& mutex_;
  pthread_cond_t pcond_;
};

}  // namespace chi
#endif
